home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Grab Bag
/
Shareware Grab Bag.iso
/
090
/
cmln0886.arc
/
SCHEME4.LTG
< prev
next >
Wrap
Text File
|
1986-06-24
|
1KB
|
37 lines
Listing 2
;;; Prime Test:
;;; A PC SCHEME test for primality by finding the smallest integral
;;; divisor of a given number N. The program tests N for divisibility
;;; by successive integers, starting with 2.
;;;
;;; To test N, enter (prime? N)
;;; To test duration, enter (timed-prime-test N).
;;╗ Thσ listinτ wil∞ prin⌠ N¼ checδ t∩ seσ iµ i⌠ i≤ prime¼ anΣ ì
;;; prin⌠ thσ tes⌠ duratioε in microseconds.
(define (square X) (* X X))
(define (smallest-divisor N)
(find-divisor N 2))
(define (find-divisor N test-divisor)
(cond (( > (square test-divisor) N) N)
((divides? test-divisor N) test-divisor)
(else (find-divisor N ( + test-divisor 1)))))
(define (divides? a b)
(= (remainder b a) 0))
(define (prime? N)
(= N (smallest-divisor N)))
(define (timed-prime-test N)
(define start-time (runtime))
(define found-prime? (prime? N))
(define elapsed-time (- (runtime) start-time))
(print N)
(cond (found-prime?
(print " TIME IN MICROSECONDS ")
(print elapsed-time))))